{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "49fb5fe1",
   "metadata": {},
   "source": [
    "## 测试 rust 版本 czsc 库的使用\n",
    "\n",
    "----"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36795078",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from datetime import datetime, timedelta\n",
    "from rs_czsc import RawBar, Freq, CZSC, Direction, Mark, format_standard_kline\n",
    "\n",
    "def create_test_bars(count=1000):\n",
    "    \"\"\"创建测试用的K线数据\"\"\"\n",
    "    # from czsc import mock\n",
    "\n",
    "    # df = mock.generate_klines()\n",
    "    df = pd.read_feather(r\"A:\\桌面临时数据\\行情数据\\BTCUSDT5分钟行情.feather\")\n",
    "    df = df.reset_index(drop=True)\n",
    "    bars = format_standard_kline(df.tail(count))\n",
    "    return bars\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07df3391",
   "metadata": {},
   "source": [
    "### 一、性能测试\n",
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe826538",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bbe13c1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "863b5fc9",
   "metadata": {},
   "source": [
    "### 二、功能测试\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "807fc39e",
   "metadata": {},
   "source": [
    "#### 测试 Signal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "49d480a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Signal('30分钟_D1_表里关系V230101_向上_任意_任意_0')\n",
      "Signal('60分钟_D1_表里关系V230101_向上_任意_任意_0')\n",
      "Signal('30分钟_D1_表里关系V230101_向上_任意_任意_0')\n"
     ]
    }
   ],
   "source": [
    "from rs_czsc import Signal\n",
    "\n",
    "s1 = Signal(key=\"30分钟_D1_表里关系V230101\", value=\"向上_任意_任意_0\")\n",
    "print(s1)\n",
    "\n",
    "s2 = Signal(k1=\"60分钟\", k2=\"D1\", k3=\"表里关系V230101\", v1=\"向上\", v2=\"任意\", v3=\"任意\", score=0)\n",
    "print(s2)\n",
    "\n",
    "s3 = Signal(\"30分钟_D1_表里关系V230101_向上_任意_任意_0\")\n",
    "print(s3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c16058b7",
   "metadata": {},
   "outputs": [
    {
     "ename": "AssertionError",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mAssertionError\u001b[39m                            Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[2]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28mstr\u001b[39m(s1) == \u001b[33m\"\u001b[39m\u001b[33m30分钟_D1_表里关系V230101_向上_任意_任意_0\u001b[39m\u001b[33m\"\u001b[39m\n",
      "\u001b[31mAssertionError\u001b[39m: "
     ]
    }
   ],
   "source": [
    "assert str(s1) == \"30分钟_D1_表里关系V230101_向上_任意_任意_0\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9260a77e",
   "metadata": {},
   "source": [
    "#### 测试 Event 对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "51911a88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '平多#A547', 'operate': 'LE', 'signals_all': ['日线_D0停顿分型_BE辅助V230106_看空_强_任意_0'], 'signals_any': [], 'signals_not': []} \n",
      "\n",
      "\n",
      "Event[平多#A547]: LE (all:1, any:0, not:0)\n"
     ]
    }
   ],
   "source": [
    "# python 版本：https://github.com/waditu/czsc/blob/0.9.68/czsc/objects.py#L630\n",
    "from rs_czsc import Event, Signal, Operate\n",
    "\n",
    "event = Event(name='平多', operate=Operate.LE, \n",
    "              signals_all=[Signal(\"日线_D0停顿分型_BE辅助V230106_看空_强_任意_0\")])\n",
    "\n",
    "e1 = event.dump()\n",
    "print(e1, \"\\n\\n\")\n",
    "e2 = Event.load(e1)\n",
    "print(e2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "35e97a59",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'builtins.Event' object has no attribute 'opens'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mAttributeError\u001b[39m                            Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[16]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43me2\u001b[49m\u001b[43m.\u001b[49m\u001b[43mopens\u001b[49m[\u001b[32m0\u001b[39m].signals_all\n",
      "\u001b[31mAttributeError\u001b[39m: 'builtins.Event' object has no attribute 'opens'"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f24f56f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from rs_czsc import Operate\n",
    "\n",
    "op = Operate.LE\n",
    "assert op.value == \"平多\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f52fa016",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'symbol': 'xxx', 'name': '30分钟_LONG_E001', 'opens': [{'name': 'LO#6B06', 'operate': 'LO', 'signals_all': [{'key': '30分钟_D1_表里关系V230101', 'value': '向上_任意_任意_0'}], 'signals_any': [], 'signals_not': [{'key': '30分钟_D1_涨跌停V230331', 'value': '涨停_任意_任意_0'}]}], 'exits': [], 'interval': 14400, 'timeout': 16, 'stop_loss': 300.0, 'T0': False, 'pairs': [], 'holds': []} \n",
      "\n",
      "\n",
      "Position(name='30分钟_LONG_E001', symbol='xxx', opens=1, exits=0)\n",
      "{'symbol': 'xxx', 'name': '30分钟_LONG_E001', 'opens': [{'name': 'LO#6B06', 'operate': 'LO', 'signals_all': [{'key': '30分钟_D1_表里关系V230101', 'value': '向上_任意_任意_0'}], 'signals_any': [], 'signals_not': []}], 'exits': [], 'interval': 14400, 'timeout': 16, 'stop_loss': 300.0, 'T0': False, 'pairs': [], 'holds': []}\n"
     ]
    }
   ],
   "source": [
    "from rs_czsc import Event, Signal, Operate, Position\n",
    "\n",
    "\n",
    "base_freq = '30分钟'\n",
    "\n",
    "opens = [\n",
    "    {\n",
    "        \"operate\": \"开多\",\n",
    "        \"signals_all\": [f\"{base_freq}_D1_表里关系V230101_向上_任意_任意_0\"],\n",
    "        \"signals_any\": [],\n",
    "        \"signals_not\": [f\"{base_freq}_D1_涨跌停V230331_涨停_任意_任意_0\"]\n",
    "    },\n",
    "]\n",
    "\n",
    "exits = []\n",
    "\n",
    "pos = Position(\n",
    "    name=f\"{base_freq}_LONG_E001\",\n",
    "    symbol=\"xxx\",\n",
    "    opens=[Event.load(x) for x in opens],\n",
    "    exits=[Event.load(x) for x in exits],\n",
    "    interval=3600 * 4,\n",
    "    timeout=16,\n",
    "    stop_loss=300,\n",
    ")\n",
    "\n",
    "p1 = pos.dump()\n",
    "print(p1, \"\\n\\n\")\n",
    "p2 = Position.load(p1)\n",
    "print(p2)\n",
    "print(p2.dump())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d741c301",
   "metadata": {},
   "outputs": [],
   "source": [
    "import hashlib\n",
    "open_signals_all = [\n",
    "    f\"{base_freq}_D1DIF10斜率T80_BS辅助V231019_看多_任意_任意_0\"\n",
    "]\n",
    "open_signals_all = [Signal(s) for s in open_signals_all]\n",
    "open_signals_all = [{\"key\": s.key, \"value\": s.value} for s in open_signals_all]\n",
    "pos_dict = pos.dump()\n",
    "sig_hash = hashlib.md5(f\"{open_signals_all}\".encode('utf-8')).hexdigest()[:8].upper()\n",
    "pos_dict['name'] = f\"{pos.name}#{sig_hash}\"\n",
    "pos_dict['opens'][0]['signals_all'].extend(open_signals_all)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4114ba96",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'str' object cannot be converted to 'PyDict'",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mTypeError\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[23]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m      1\u001b[39m pos_dict\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m \u001b[43mPosition\u001b[49m\u001b[43m.\u001b[49m\u001b[43mload\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpos_dict\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\zengb\\.conda\\envs\\CZSC10\\Lib\\site-packages\\rs_czsc\\_objects\\position.py:93\u001b[39m, in \u001b[36mPosition.load\u001b[39m\u001b[34m(cls, data)\u001b[39m\n\u001b[32m     90\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(data, \u001b[38;5;28mdict\u001b[39m):\n\u001b[32m     91\u001b[39m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mPosition.load expects dict or JSON string, got \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(data)\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m\"\u001b[39m)\n\u001b[32m---> \u001b[39m\u001b[32m93\u001b[39m inner = \u001b[43mPyPosition\u001b[49m\u001b[43m.\u001b[49m\u001b[43mload\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m     94\u001b[39m wrapper = \u001b[38;5;28mcls\u001b[39m.\u001b[34m__new__\u001b[39m(\u001b[38;5;28mcls\u001b[39m)\n\u001b[32m     95\u001b[39m wrapper._inner = inner\n",
      "\u001b[31mTypeError\u001b[39m: 'str' object cannot be converted to 'PyDict'"
     ]
    }
   ],
   "source": [
    "pos_dict\n",
    "Position.load(pos_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c02981e3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "CZSC10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
